package com.msl.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.base.BaseActivity;
import com.msl.bean.Ab1Bean;
import com.msl.tool.ToastUtils;
import com.seuic.misc.Misc;
import java.io.IOException;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class ConfigActivity extends BaseActivity {
    private String tag="ConfigActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.ivu)
    ImageView ivu;
    @Bind(R.id.login_id)
    EditText loginId;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.tvr)
    TextView tvr;
    @Bind(R.id.loginbt)
    Button loginbt;
    String cid="";
    String add="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
        ButterKnife.bind(this);
        tvTitle.setText("网络配置");
        loginId.setText(MslApp.sSharedPreferences.getString(MslApp.cidk,""));
        loginP.setText(MslApp.sSharedPreferences.getString(MslApp.addk,"").replace("http://",""));
//        loginId.setText("3070");
//        loginP.setText("mls.zaosin.cn");
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.loginbt:
                cid= loginId.getText().toString();
                if(cid.length()==0)
                {
                    ToastUtils.showSingleToast(this,"请填写企业编号");
                    return;
                }
                add= loginP.getText().toString();
                if(add.length()==0)
                {
                    ToastUtils.showSingleToast(this,"请填写服务器地址");
                    return;
                }
                waitDialog.show();
                Misc misc = new Misc();
                config("http://"+add+MslApp.address+"?action=CheckNet&cid="+loginId.getText().toString()+"&sn="+misc.getSN());
//                config("http://"+add+MslApp.address+"?action=CheckNet&cid="+loginId.getText().toString()+"&sn=90P8F0944000");
//                config("http://"+add+MslApp.address+"?action=CheckNet&cid="+loginId.getText().toString()+"&sn=dealerweb");
                break;
        }
    }

    public void config(String url)
    {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.isSuccessful()){//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag,"result="+result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    }
                    else
                    {
                        Log.e(tag,"获取数据失败了"+response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
            waitDialog.dismiss();
        }

    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                    try {
                        Ab1Bean ab = gson.fromJson(result, Ab1Bean.class);
                        if(ab.getErrcode()==0)
                        {
                            tvr.setText("连接成功");
                            MslApp.sEditor.putString(MslApp.addk, "http://"+add);
                            MslApp.sEditor.putString(MslApp.cidk, cid);
                            MslApp.sEditor.commit();
                        }
                        else
                        {
                            tvr.setText("连接失败");
                        }
                    }
                    catch (Exception e)
                    {
                        tvr.setText("连接失败");
                    }
                    finally {
                    }
                    break;
                default:
                    break;
            }
        }
    };


}
